<template>
	<div class="app-container calendar-list-container">
		<div class="filter-container">
			<el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="城市错误码" v-model.trim="codeOperation.codeid"> </el-input>
			<el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="支付宝错误码" v-model.trim="codeOperation.resultCode"> </el-input>
			<el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="错误描述" v-model.trim="codeOperation.resultDescription"> </el-input>
			<!-- <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="操作时间" v-model="codeOperation.operateDate"> </el-input> -->
			<el-date-picker style="width: 200px;" class="filter-item" v-model="getoperateDate" formatter="yyyy-MM-dd" @change="getSTime" type="date" placeholder="操作时间"></el-date-picker>
			<el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
			<el-button class="filter-item" v-if="codeOperationManager_btn_add" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>

		</div>
		<div class="filter-container">
			<input v-if="codeOperationManager_btn_uploading" class="filter-item" type="file" @change="getFile($event)">
			<el-button  v-if="codeOperationManager_btn_uploading" class="filter-item" style="margin-left: 10px;" @click="submit($event)" type="primary" icon="">上传</el-button>
			<a v-if="codeOperationManager_btn_download" href="/api/codeOperation/excle">
				<el-button class="filter-item" style="margin-left: 10px;" type="primary">模板下载
				</el-button>
			</a>

		</div>
		<el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">

			<el-table-column type="index" label="序号" header-align="center" align="center" width="65">
				<template scope="scope"><span>{{scope.$index+(codeOperation.page - 1) * codeOperation.limit + 1}} </span></template>
			</el-table-column>
			<el-table-column width="200px" align="center" label="城市错误码">
				<template scope="scope">
					<span>{{scope.row.codeid}}</span>
				</template>
			</el-table-column>
			<el-table-column width="200px" align="center" label="支付宝错误码">
				<template scope="scope">
					<span>{{scope.row.resultCode}}</span>
				</template>
			</el-table-column>

			<el-table-column width="300px" align="center" label="错误描述">
				<template scope="scope">
					<span>{{scope.row.resultDescription}}</span>
				</template>
			</el-table-column>
			<el-table-column width="200px" align="center" label="状态">
				<template scope="scope">
					<span v-if="scope.row.codeState == '1'">有效</span>
					<span v-if="scope.row.codeState == '2'">无效</span>
				</template>
			</el-table-column>
			<el-table-column width="200px" align="center" label="操作时间">
				<template scope="scope">
					<span>{{scope.row.operateDate}}</span>
				</template>
			</el-table-column>
			<el-table-column fixed="right" align="center" label="操作" width="200">
				<template scope="scope">
					<el-button v-if="codeOperationManager_btn_edit" size="small" type="success" @click="handleUpdate(scope.row)">编辑
					</el-button>
					<el-button v-if="codeOperationManager_btn_view" size="small" type="success" @click="handleRedis(scope.row)">redis
					</el-button>
					<el-button v-if="codeOperationManager_btn_del" size="small" type="danger" @click="handleDelete(scope.row)">作废
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div v-show="!listLoading" class="pagination-container">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="codeOperation.page" :page-sizes="[10,20,30, 50]" :page-size="codeOperation.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
		</div>
		<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
			<el-form :model="form" :rules="rules" ref="form" label-width="100px">
				<el-form-item label="城市错误码" prop="codeid">
					<el-input v-model="form.sql.codeid" placeholder="请输入城市错误码"></el-input>
				</el-form-item>
				<el-form-item label="支付宝错误码" prop="resultCode">
					<el-input v-model="form.sql.resultCode" placeholder="请输入支付宝错误码"></el-input>
				</el-form-item>
				<el-form-item label="结果状态" prop="resultStats">
					<el-input v-model="form.sql.resultStats" placeholder="请输入S成功F失败"></el-input>
				</el-form-item>
				<el-form-item label="错误描述" prop="resultDescription">
					<el-input v-model="form.sql.resultDescription" placeholder="请输入错误描述"></el-input>
				</el-form-item>

				<el-form-item label="状态" prop="codeState">
					<!-- <el-input v-model="form.codeState" placeholder="请输入1有效2无效"></el-input> -->
					<el-select v-model="ztAddOpton" @change="getztSel" placeholder="请选择状态">
						<el-option v-for="item in ztAddSel" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="备用1" prop="flg1">
					<el-input v-model="form.sql.flg1" placeholder="请输入备用1"></el-input>
				</el-form-item>
				<el-form-item label="备用2" prop="flg2">
					<el-input v-model="form.sql.flg2" placeholder="请输入备用2"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancel('form')">取 消</el-button>
				<el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
				<el-button v-else type="primary" @click="update('form')">确 定</el-button>
			</div>
		</el-dialog>
		<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisiblelView">
			<el-form :model="form" :rules="rules" ref="form" label-width="110px">
				
				<el-form-item v-if="dialogStatus=='view'" label="redis緩存数据" prop="codeid" style="margin-bottom:0px">
					<el-input v-model="form.redis.codeid" readonly></el-input>
				</el-form-item>
				<el-form-item label="城市错误码" prop="codeid">
					<el-input v-model="form.sql.codeid" placeholder="请输入城市错误码" readonly></el-input>
				</el-form-item>
				
				<el-form-item v-if="dialogStatus=='view'" label="redis緩存数据" style="margin-bottom:0px" prop="resultCode">
					<el-input v-model="form.redis.resultCode" readonly></el-input>
				</el-form-item>
				<el-form-item label="支付宝错误码" prop="resultCode">
					<el-input v-model="form.sql.resultCode" readonly></el-input>
				</el-form-item>
				
				<el-form-item v-if="dialogStatus=='view'" label="redis緩存数据" style="margin-bottom:0px" prop="resultStats">
					<el-input v-model="form.redis.resultStats" readonly></el-input>
				</el-form-item>
				<el-form-item label="结果状态" prop="resultStats">
					<el-input v-model="form.sql.resultStats" readonly></el-input>
				</el-form-item>
				
				<el-form-item v-if="dialogStatus=='view'" label="redis緩存数据" style="margin-bottom:0px" prop="resultDescription">
					<el-input v-model="form.redis.resultDescription" readonly></el-input>
				</el-form-item>
				<el-form-item label="错误描述" prop="resultDescription">
					<el-input v-model="form.sql.resultDescription" readonly></el-input>
				</el-form-item>

				<el-form-item v-if="dialogStatus=='view'" label="redis緩存数据" style="margin-bottom:0px" prop="codeState">
					<!-- <el-input v-model="form.codeState" placeholder="请输入1有效2无效"></el-input> -->
					<el-select v-model="ztAddOptonRedis" @change="getztSel" :disabled=true>
						<el-option v-for="item in ztAddSel" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="状态" prop="codeState">
					<!-- <el-input v-model="form.codeState" placeholder="请输入1有效2无效"></el-input> -->
					<el-select v-model="ztAddOpton" @change="getztSel" :disabled=true>
						<el-option v-for="item in ztAddSel" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				
				<el-form-item v-if="dialogStatus=='view'" label="redis緩存数据" style="margin-bottom:0px" prop="flg1">
					<el-input v-model="form.redis.flg1" readonly></el-input>
				</el-form-item>
				<el-form-item label="备用1" prop="flg1">
					<el-input v-model="form.sql.flg1" readonly></el-input>
				</el-form-item>
				
				<el-form-item v-if="dialogStatus=='view'" label="redis緩存数据" style="margin-bottom:0px" prop="flg2">
					<el-input v-model="form.redis.flg2" readonly></el-input>
				</el-form-item>
				<el-form-item label="备用2" prop="flg2">
					<el-input v-model="form.sql.flg2" readonly></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="cancelView('form')">确 定</el-button>
				<el-button type="danger" @click="deleteRedis('form')">删除redis</el-button>
			</div>
		</el-dialog>
		<el-button type="primary" element-loading-text="正在上传..." element-loading-background="rgba(0, 0, 0, 0.7)" v-loading.fullscreen.lock="fullscreenLoading"></el-button>
	</div>
</template>

<script>
	import {
		page,
		addObj,
		getObj,
		getObjRedis,
		deleteRedis,
		delObj,
		putObj
	} from 'api/admin/codeOperation/index';
	import {
		getToken
	} from 'utils/auth';
	import { mapGetters } from 'vuex';
	import axios from 'axios';
	export default {
		name: 'codeOperation',
		data() {
			return {
				fullscreenLoading: false,
				getoperateDate: '',
				ztAddSel: [{
					value: '1',
					label: '有效'
				}, {
					value: '2',
					label: '无效'
				}],
				ztAddOpton: '',
				ztAddOptonRedis: '',
				form: {
					sql: {
						codeid: undefined,
						resultCode: undefined,
						resultStats: undefined,
						resultDescription: undefined,
						operateDate: undefined,
						codeState: undefined,
						flg1: undefined,
						flg2: undefined
					},
					redis: {
						codeid: undefined,
						resultCode: undefined,
						resultStats: undefined,
						resultDescription: undefined,
						operateDate: undefined,
						codeState: undefined,
						flg1: undefined,
						flg2: undefined
					}
				},
				rules: {
					/*
					    codeid: [
					  {
					    required: true,
					    message: '请输入城市错误码',
					    trigger: 'blur'
					  },
					  {
					    min: 3,
					    max: 20,
					    message: '长度在 3 到 20 个字符',
					    trigger: 'blur'
					  }
					],   resultCode: [
					  {
					    required: true,
					    message: '请输入支付宝错误码',
					    trigger: 'blur'
					  },
					  {
					    min: 3,
					    max: 20,
					    message: '长度在 3 到 20 个字符',
					    trigger: 'blur'
					  }
					],   resultStats: [
					  {
					    required: true,
					    message: '请输入S成功F失败',
					    trigger: 'blur'
					  },
					  {
					    min: 3,
					    max: 20,
					    message: '长度在 3 到 20 个字符',
					    trigger: 'blur'
					  }
					],   resultDescription: [
					  {
					    required: true,
					    message: '请输入错误描述',
					    trigger: 'blur'
					  },
					  {
					    min: 3,
					    max: 20,
					    message: '长度在 3 到 20 个字符',
					    trigger: 'blur'
					  }
					],   operateDate: [
					  {
					    required: true,
					    message: '请输入操作时间',
					    trigger: 'blur'
					  },
					  {
					    min: 3,
					    max: 20,
					    message: '长度在 3 到 20 个字符',
					    trigger: 'blur'
					  }
					],   codeState: [
					  {
					    required: true,
					    message: '请输入1有效2无效',
					    trigger: 'blur'
					  },
					  {
					    min: 3,
					    max: 20,
					    message: '长度在 3 到 20 个字符',
					    trigger: 'blur'
					  }
					],   flg1: [
					  {
					    required: true,
					    message: '请输入备用1',
					    trigger: 'blur'
					  },
					  {
					    min: 3,
					    max: 20,
					    message: '长度在 3 到 20 个字符',
					    trigger: 'blur'
					  }
					],   flg2: [
					  {
					    required: true,
					    message: '请输入备用2',
					    trigger: 'blur'
					  },
					  {
					    min: 3,
					    max: 20,
					    message: '长度在 3 到 20 个字符',
					    trigger: 'blur'
					  }
					]      */
				},
				list: null,
				total: null,
				listLoading: true,
				tableName: '',
				codeOperation: {
					page: 1,
					limit: 10
				},
				dialogFormVisible: false,
				dialogFormVisiblelView: false,
				dialogStatus: '',
				codeOperationManager_btn_edit: false,
				codeOperationManager_btn_del: false,
				codeOperationManager_btn_add: false,
				codeOperationManager_btn_view: false,
				codeOperationManager_btn_download: false,
				codeOperationManager_btn_uploading: false,
				textMap: {
					update: '编辑',
					create: '创建',
					view: '查看'
				},
				tableKey: 0
			}
		},
		created() {
			this.getList();
			this.codeOperationManager_btn_edit = this.elements['codeOperationManager:btn_edit'];
			this.codeOperationManager_btn_del = this.elements['codeOperationManager:btn_del'];
			this.codeOperationManager_btn_add = this.elements['codeOperationManager:btn_add'];
			this.codeOperationManager_btn_view = this.elements['codeOperationManager:btn_view'];
			this.codeOperationManager_btn_download = this.elements['codeOperationManager:btn_download'];
			this.codeOperationManager_btn_uploading = this.elements['codeOperationManager:btn_uploading'];
		},

		computed: {
			...mapGetters([
				'elements'
			])
		},
		methods: {
			getList() {
				this.listLoading = true;
				page(this.codeOperation)
					.then(response => {
						this.list = response.data.rows;
						this.total = response.data.total;
						this.listLoading = false;
					})
			},
			getFile: function(event) {
				this.file = event.target.files[0];
				console.log(this.file);
			},
			submit: function(event) {
				//阻止元素发生默认的行为
				event.preventDefault();
				let upFile = new FormData();
				upFile.append("file", this.file);
				let config = {
					headers: {
						'Content-Type': 'multipart/form-data' //之前说的以表单传数据的格式来传递fromdata
					}
				};
				this.$confirm('此操作将上传文件, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					let that = this;
					if(that.file) {
						that.fullscreenLoading = true;
						config.headers['Authorization'] = getToken();
						axios.post('/api/codeOperation/importExcel', upFile, config)
							.then(function(response) {
								that.fullscreenLoading = false;
								alert(response.data.message);
								window.location.reload();
							})
							.catch(function(error) {
								that.fullscreenLoading = false;
								alert("上传失败");
								console.log(error);
								// window.location.reload();
							})
					} else {
						alert("请选择文件");
					}

				});
			},
			handleFilter() {
				this.getList();
			},
			handleSizeChange(val) {
				this.codeOperation.limit = val;
				this.getList();
			},
			handleCurrentChange(val) {
				this.codeOperation.page = val;
				this.getList();
			},
			// 创建时 获取状态的值
			getztSel(val) {
				this.form.codeState = val
			},
			// 日期插件增加的方法 修改日期格式
			getSTime(val) {
				this.getoperateDate = val
				this.codeOperation.operateDate = val;
			},
			handleCreate() {
				this.ztAddOpton = ''
				this.resetTemp();
				this.dialogStatus = 'create';
				this.dialogFormVisible = true;
			},
			handleUpdate(row) {
				this.ztAddOpton = ''
				getObj(row.id)
					.then(response => {
						this.form.sql = response.data;
						this.ztAddOpton = this.form.sql.codeState
						this.dialogFormVisible = true;
						this.dialogStatus = 'update';
					});
			},
			handleRedis(row) {
				this.form.sql = row;
				this.ztAddOpton = row.codeState
				this.tableName = 'code_operation';
				getObjRedis(this.form.sql, this.tableName)
					.then(response => {
						if(response.status == 0) {
							this.form.redis = response.data;
							this.ztAddOptonRedis = response.data.codeState
							//        	this.form.redis = this.form.sql;
							this.dialogStatus = 'view';
							this.dialogFormVisiblelView = true;
						} else {
							this.$notify({
								title: '查询失败：',
								message: response.message,
								type: 'warning',
								duration: 3000

							});
						}
					});

			},
			handleDelete(row) {
				this.$confirm('此操作将作废这条数据, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						delObj(row)
							.then((result) => {
								if(result.status == 0) {
									this.dialogFormVisible = false;
									const index = this.list.indexOf(row);
									this.list.splice(index, 1);
									this.$notify({
										title: '成功',
										message: result.message,
										type: 'success',
										duration: 2000
									});
								} else {
									this.$notify({
										title: '失败',
										message: result.message,
										type: 'warning',
										duration: 2000
									});
								};
							});
					});
			},
			create(formName) {
				const set = this.$refs;
				set[formName].validate(valid => {
					if(valid) {
						this.form.sql.codeState=this.ztAddOpton 
						addObj(this.form.sql)
							.then((result) => {
								if(result.status === 1) {
									this.$message({
										message: '城市错误码不能为空！',
										type: 'warning',
										duration: '2000',
									})
								} else if(result.status === 2) {
									this.$message({
										message: '城市错误码已存在！',
										type: 'warning',
										duration: '2000',
									})
								} else if(result.status == 0) {
									this.dialogFormVisible = false;
									this.getList();
									this.$notify({
										title: '成功',
										message: result.message,
										type: 'success',
										duration: 2000
									});
								} else {
									this.$notify({
										title: '失败',
										message: result.message,
										type: 'warning',
										duration: 2000
									});
								}

							})
					} else {
						return false;
					}
				});
			},
			cancel(formName) {
				this.dialogFormVisible = false;
				const set = this.$refs;
				set[formName].resetFields();
			},
			cancelView(formName) {
				this.dialogFormVisiblelView = false;
				const set = this.$refs;
				set[formName].resetFields();
			},
			update(formName) {
				const set = this.$refs;
				set[formName].validate(valid => {
					if(valid) {
						//						this.dialogFormVisible = false;
						this.form.password = undefined;
						putObj(this.form.sql.id, this.form.sql).then((result) => {
							if(result.status === 1) {
								this.$message({
									message: '城市错误码已存在！',
									type: 'warning',
									duration: '2000',
								})
							} else if(result.status == 0) {
								this.dialogFormVisible = false;
								this.getList();
								this.$notify({
									title: '成功',
									message: result.message,
									type: 'success',
									duration: 2000
								});
							} else {
								this.$notify({
									title: '失败',
									message: result.message,
									type: 'warning',
									duration: 2000
								});
							}
						});
					} else {
						return false;
					}
				});
			},
			deleteRedis(formName) {
				this.$confirm('此操作将删除redis数据?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					const set = this.$refs;
					set[formName].validate(valid => {
						if(valid) {
							this.form.password = undefined;
							this.tableName = 'code_operation';
							deleteRedis(this.form.sql, this.tableName).then((result) => {
								this.dialogFormVisiblelView = false;
								this.getList();
								if(result.status == 0) {
									this.$notify({
										title: '操作成功',
										message: result.message,
										type: 'success',
										duration: 3000
									});
								} else {
									this.$notify({
										title: '操作失败:',
										message: result.message,
										type: 'warning',
										duration: 3000
									});
								}
								/*this.$notify({
								title: '成功',
								message: '更新成功',
								type: 'success',
								duration: 2000
								});*/
							});
						} else {
							return false;
						}
					});
				});
			},
			resetTemp() {
				this.form.sql= {
					username: undefined,
					name: undefined,
					sex: '男',
					password: undefined,
					description: undefined
				};
			}
		}
	}
</script>